<template>
<el-container>
  <el-header style="background-color: white; color: black;">
      <h1 align="center">仓库操作</h1>
  </el-header>
  <el-main>
    <!-- 查询条件表单 -->
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="位置">
        <el-input v-model="queryForm.storageAddr" placeholder="位置"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-input v-model="queryForm.storageType" placeholder="类型"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadData()">查询</el-button>
        <el-button type="primary" @click="addStorage()">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="100%" >
      <!-- 列组件 -->
      <!-- prop 对应当前页集合的对象属性-->
      <el-table-column prop="storageId" label="编号" style="0%"></el-table-column>
      <el-table-column prop="storageAddr" label="仓库位置" style="0%"></el-table-column>
      <el-table-column prop="storageSize" label="仓库大小" style="0%"></el-table-column>
      <el-table-column prop="storageType" label="仓库类型" style="0%"></el-table-column>
      <el-table-column prop="storageRemark" label="仓库备注" style="0%"></el-table-column>
      <el-table-column label="操作" style="0%">
          <!-- slot-scope 插槽: 用来获取循环的迭代变量-->
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="update(scope.row,scope.$index)">修改</el-button>
            <el-button type="danger" size="mini" @click="deleteStorage(scope.row,scope.$index)">删除</el-button>
          </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
    @size-change="select"
    @current-change="selectPage"
    :current-page="queryForm.page"
    :page-sizes="[10,15,20]"
    :page-size="queryForm.row"
    :total="total"
    layout="total,sizes,prev,pager,next,jumper"
    >
    </el-pagination>
    <el-dialog :title="title" :visible.sync="isShow" width="50%">
        <el-form :model="storageForm" status-icon  ref="storageForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="仓库地址" prop="suppliersName">
                <!-- <el-input v-model="storageForm.storageAddr" ></el-input> -->
                <el-cascader size="large" :options="options" v-model="selectedOptions" @change="handleChange">
                </el-cascader>
            </el-form-item>
            <el-form-item label="仓库大小"  prop="suppliersName">
                <el-input v-model="storageForm.storageSize" ></el-input>
            </el-form-item>
            <el-form-item label="仓库类型" prop="suppliersName">
                <el-input v-model="storageForm.storageType" ></el-input>
            </el-form-item>
            <el-form-item label="仓库备注" prop="suppliersName">
                <el-input v-model="storageForm.storageRemark" ></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="submitForm(index)">提交</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
  </el-main>
</el-container>
</template>

<script>
  import {regionData,CodeToText,TextToCode} from 'element-china-area-data'
  export default {
      data() {
        return {
          options: regionData,
          selectedOptions: [],
          tableData: [],
          totalPage: 0, //总页数
          total: 0, //总条数
          title:"修改",
          isShow:false,
          index:0 ,  //0为修改  1为添加
          queryForm: {
            page: 1,
            row: 10,
            storageAddr:"",
            storageType:""
          },
          storageForm: {
            storageId:0,
            storageAddr:"",
            storageSize:"",
            storageType:"",
            storageRemark:"",
          },
        }
      },
      methods: {
        loadData() { //加载数据
          var self = this;
          var http = this.$http.post("/api/storage/storageIndex",this.$qs.stringify(this.queryForm));
          http.then(function(rs) {
            if (rs.data.code == 200) {
              self.tableData = rs.data.data.data;
              self.total = rs.data.data.total;
              self.totalPage = rs.data.data.totalPage;
            }else{
              self.tableData=[];
              self.total=0;
            }
          })
        },
        select(row){ //分页下拉框选中触发的事件，row是当前选中条数
          this.queryForm.row=row;
          //刷新
          this.loadData();
        },
        selectPage(page){//选中页码触发的事件, page就是选中的页码
          this.queryForm.page = page;
          //刷新
          this.loadData();
        },
        handleChange(value) {
          this.storageForm.storageAddr += CodeToText[value[0]]+","+
                                          CodeToText[value[1]]+","+
                                          CodeToText[value[2]]
        },
        update(x,index) { //修改回显
          this.selectedOptions={};
          this.title = "修改"
          this.storageForm = x;
          this.isShow = true;
          this.index = 0
        },
        deleteStorage(x,index) { //删除
          this.storageForm = x;
          var self = this
          var http = this.$http.post("api/storage/deleteStorage",this.$qs.stringify(this.storageForm));
          http.then(function(rs){
              if(rs.data.code == 200){
                alert(rs.data.msg)
                location.reload();
              }
          })
        },
        addStorage(){
          this.title = "添加"
          this.isShow = true;
          this.storageForm={};
          this.storageForm.storageAddr="";
          this.index = 1
        },
        submitForm(i){
          if(i == 0){
            var self = this
            var http = this.$http.post("/api/storage/storageUpdate",this.$qs.stringify(this.storageForm));
            http.then(function(rs){
                if(rs.data.code == 200){
                  alert(rs.data.msg)
                  location.reload();
                }else{
                  alert(rs.data.msg)
                }
            })
          }else{
            var self = this
            var http = this.$http.post("api/storage/addStorage",this.$qs.stringify(this.storageForm));
            http.then(function(rs){
                if(rs.data.code == 200){
                  alert(rs.data.msg)
                  location.reload();
                }else{
                  alert(rs.data.msg)
                }
            })
          }
        }
      },
      mounted(){
        this.loadData();
      }
    }
</script>

<style>
  .el-table .warning-row {
      background: oldlace;
    }

    .el-table .success-row {
      background: #f0f9eb;
    }
</style>
